﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace power_car.Tool
{
    class MySqlConn
    {
        #region  建立MySql数据库连接
        ///<summary>
        ///建立数据库连接.
        /// </summary>
        /// <returns>返回MySqlConnection对象</returns>
        public MySqlConnection getMySqlCon()
        {
            string M_str_sqlcon = "server=rm-bp1wbi31moya579yswo.mysql.rds.aliyuncs.com;User id=root;password=Hb123456;database=game"; //根据自己的设置
            MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
            return myCon;
        }
        #endregion

        #region  新增、修改、删除
        /// <summary>
        /// 执行MySqlCommand
        /// </summary>
        /// <param name="M_sql">SQL语句</param>
        public void do_add(string M_sql)
        {
            MySqlConnection conn = this.getMySqlCon();
            conn.Open();
            try
            {
                MySqlCommand mysqlcom = new MySqlCommand(M_sql, conn);
                mysqlcom.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region  查找
        /// <summary>
        /// 执行MySqlCommand
        /// </summary>
        /// <param name="M_query_sql">新增SQL语句</param>
        public void do_query_one(string M_query_sql)
        {
            List<MySqlDataReader> list = new List<MySqlDataReader>();
            MySqlConnection conn = this.getMySqlCon();
            conn.Open();
            try
            {
                MySqlCommand mysqlcom = new MySqlCommand(M_query_sql, conn);
                MySqlDataReader dataReader = mysqlcom.ExecuteReader();
                while (dataReader.Read())
                {
                    Console.WriteLine("获取到的信息是userId:" + dataReader["user_id"] + ",userName:" + dataReader["user_name"] + ",userPwd:" + dataReader["user_pwd"]);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region  查找
        /// <summary>
        /// 执行MySqlCommand
        /// </summary>
        /// <param name="M_query_sql">新增SQL语句</param>
        public DataSet do_query_list(string M_query_sql)
        {
            // 创建数据存放集合;
            DataSet data = new DataSet();
            MySqlConnection conn = this.getMySqlCon();
            conn.Open();
            try
            {
                MySqlDataAdapter MyAd = new MySqlDataAdapter(M_query_sql, conn);
                // 通过数据适配器读取数据并导入到数据集合;
                MyAd.Fill(data);
                conn.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return data;
        }
        #endregion
    }
}
